<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/5
 * Time: 11:32
 */

namespace app\common\model;
use think\Db;
class Log
{

    /**日志记录
     * @param $msg
     * @param string $info
     * @param string $path
     */

    static public function record($msg,$info='log',$path=''){

        if(IS_CLI){
            $class=config('cli_class');
            $action=config('cli_action');
            $path=LOG_BASE.'daemon'.DS.$class.DS.$action.DS;
        }else{
            $request=request();
            $method=$request->action();
            $controller=$request->controller();
            $module=$request->module();
            if(empty($path)){
                $path=LOG_BASE.$module.DS.$controller.DS.$method.DS;
            }else{
                $path=LOG_BASE.$path.DS;
            }
        }
        //$path=APP_PATH.'..'.DS.'log'.DS;
        \think\Log::init(['type'=>'File','path'=>$path]);
        \think\Log::record($msg,$info);
    }

    static public function operate($opid,$info='OP_LOG',$path=''){
        $request=request();
        $module=$request->module();
        $controller=$request->controller();
        $action=$request->action();
        $str='[ACT:'.$module.'/'.$controller.'/'.$action.']';
        $str.='[OPID:'.$opid.']';


        self::write($str,$info,'operate');

    }
    static public function write($msg,$info='log',$path=''){
        $path=LOG_BASE.$path.DS;

        \think\Log::init(['type'=>'File','path'=>$path]);
        \think\Log::write($msg,$info);
    }

    static public function notifyLog($url,$input,$output,$type,$gameId,$channelId,$myOrderId,$result,$msg){
        if(empty($myOrderId)){
            return false;
        }
        $data=[
            'url'=>$url,
            'input'=>$input,
            'output'=>$output,
            'time'=>time(),
            'type'=>$type,
            'game_id'=>$gameId,
            'channel_id'=>$channelId,
            'my_order_id'=>$myOrderId,
            'result'=>$result,
            'msg'=>$msg
        ];
        try{
            $table=get_order_table($myOrderId);
            $rs=Db::name($table)
                ->where(['my_order_id'=>$myOrderId])
                ->update(['notifyed'=>1]);
            $year=get_order_year($myOrderId);

            $db=Db::connect();
            $tables=$db->getTables(config('database.database'));
            $prefix=config('database.prefix');
            krsort($tables);
            $tableExists=false;
            foreach($tables as $v){
                if($v==$prefix.'notify_log_y_'.$year){
                    $tableExists=true;
                    break;
                }
            }
            //如果表不存在就创建
            if(!$tableExists) {
                $sql = "CREATE TABLE `{$prefix}notify_log_y_{$year}` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `url` varchar(4096) DEFAULT NULL,
  `input` varchar(4096) DEFAULT NULL,
  `output` varchar(1024) DEFAULT NULL,
  `time` int(11) DEFAULT NULL,
  `type` varchar(20) DEFAULT NULL,
  `game_id` int(10) DEFAULT NULL,
  `channel_id` int(10) DEFAULT NULL,
  `my_order_id` varchar(64) DEFAULT NULL,
  `result` varchar(64) DEFAULT NULL,
  `msg` varchar(512) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `order` (`my_order_id`),
  KEY `result` (`result`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
                Db::execute($sql);//创建表
            }
            $rs=Db::name('notify_log_y_'.$year)
                ->insert($data);
            if($rs){
                return true;
            }else{
                self::write(json_encode($data),'notify_log_fail','notify_log');
                return false;
            }
        }catch(\Exception $e){
            self::write(json_encode($data).'|'.$e->getMessage(),'notify_log_fail','notify_log');
            return false;
        }


    }
}